﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace ORMConfiger
{
    public static class GetConfigurationsByJson
    {
        private static IConfigurationRoot Configuration;

        static GetConfigurationsByJson()
        {
            BuildConfiguration();
        }

        /// <summary>
        /// 根据Json文件生成Configuration对象
        /// </summary>
        /// <param name="fileName"></param>
        static void BuildConfiguration(string fileName = "ORM.json")
        {
            Configuration = new ConfigurationBuilder().AddJsonFile(fileName).Build();
        }

        /// <summary>
        /// 获取Json文件配置的数据库连接字符串
        /// </summary>
        /// <returns></returns>
        public static string GetConnectionString()
        {
            return Configuration["ConnectionStrings:Default"];
        }

        /// <summary>
        /// 获取Json文件配置的数据库连接字符串
        /// </summary>
        /// <param name="fileName">Json文件名</param>
        /// <param name="format">格式
        /// <para>例如：ConnectionStrings:Default</para>
        /// </param>
        /// <returns></returns>
        public static string GetConnectionString(string fileName, string format)
        {
            //读取Json文件
            BuildConfiguration(fileName);
            //获取配置信息
            return Configuration[format];
        }
    }
}
